package com.ym.b2b.charge.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.charge.service.SaleDetailService;
import com.ym.b2b.charge.service.SaleSummaryService;
import com.ym.b2b.charge.vo.SaleDetailVO;
import com.ym.b2b.charge.vo.SaleSummaryVO;
import com.ym.b2b.commons.controller.BaseController;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

/**
 * @ClassName: ManageTradeChargeController
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-3-10 下午2:54:12
 * @Modify
 * @CopyRight B2B小组
 */

@Controller
@RequestMapping("/charge")
public class ManageTradeChargeController extends BaseController {

    @Autowired
    private SaleSummaryService saleSummaryService;

    @Autowired
    private SaleDetailService  saleDetailService;

    @RequestMapping("searchPageSaleSummary.do")
    @ResponseBody
    public Result searchPageSaleSummary(SaleSummaryVO saleSummaryVO, Page<SaleSummaryVO> page, HttpServletRequest req) {

        Result result = new Result();
        try {
            result = saleSummaryService.searchPageSaleSummary(saleSummaryVO, page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;

    }

    @RequestMapping(value = "searchPageSaleSummaryExcel.do")
    @ResponseBody
    public Result searchPageSaleSummaryExcel(SaleSummaryVO saleSummaryVO, Page<SaleSummaryVO> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = saleSummaryService.searchPageSaleSummary(saleSummaryVO, page);
            ExcelUtil<SaleSummaryVO> excelUtil = new ExcelUtil<SaleSummaryVO>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("saleSummaryListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping("searchPageSaleDetail.do")
    @ResponseBody
    public Result searchPageSaleDetail(SaleDetailVO saleDetailVO, Page<SaleDetailVO> page, HttpServletRequest req) {

        Result result = new Result();
        try {
            result = saleDetailService.searchPageSaleDetail(saleDetailVO, page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;

    }

    @RequestMapping(value = "searchPageSaleDetailExcel.do")
    @ResponseBody
    public Result searchPageSaleDetailExcel(SaleDetailVO saleDetailVO, Page<SaleDetailVO> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = saleDetailService.searchPageSaleDetail(saleDetailVO, page);
            ExcelUtil<SaleDetailVO> excelUtil = new ExcelUtil<SaleDetailVO>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("saleDetailListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }
}
